﻿using System.Diagnostics;
using System.Collections.Generic;
using TakePhotoTogether.Model;

namespace TakePhotoTogether.Dao
{
    public class SyncPictureDao
    {
        private const string DEBUG_TAG = "TakePhotoTogether.Dao.SyncPictureDao => ";

        private Dictionary<string, SyncPictureQueue> picturesByUser;

        private SyncPictureDao()
        {
            picturesByUser = new Dictionary<string, SyncPictureQueue>();
        }

        private static SyncPictureDao instance = null;

        public static SyncPictureDao GetInstance()
        {
            if (instance == null)
            {
                instance =  new SyncPictureDao();
            }

            return instance;
        }

        public SyncPictureQueue GetPicturesBy(string Identifier)
        {
            Debug.WriteLine(DEBUG_TAG + "GetPicturesBy - looking for identifier: " + Identifier);

            SyncPictureQueue pictures = null;

            if (picturesByUser.ContainsKey(Identifier))
            {
                pictures = picturesByUser[Identifier];
            }

            if (pictures == null)
            {
                pictures = new SyncPictureQueue();
                pictures.UserIdentifier = Identifier;
            }

            Debug.WriteLine(DEBUG_TAG + "GetPicturesBy - got queue: " + pictures);

            return pictures;
        }

        public void Save(SyncPictureQueue pictures)
        {
            if (pictures == null)
            {
                return;
            }

            picturesByUser[pictures.UserIdentifier] = pictures;
        }
    }

    public static class SyncPictureExtensions
    {
        public static void Save(this SyncPictureQueue syncPictures)
        {
            // Business rules have changed
            SyncPictureDao.GetInstance().Save(syncPictures);
        }
    }
}
